package com.cadre.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cadre.common.utils.SecurityUtils;
import com.cadre.common.utils.oldutil.ReportConfig;
import com.cadre.common.utils.oldutil.WordReplaceUtil;
import com.cadre.system.domain.officer.BatchconferenceEntity;
import com.cadre.system.domain.officer.ConfernamelistEntity;
import com.cadre.system.mapper.BatchconferenceMapper;
import com.cadre.system.mapper.NEWBatchconferenceMapper;
import com.cadre.system.mapper.NEWConfernamelistMapper;
import com.cadre.system.service.NEWBatchconferenceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

@Service("NEWBatchconferenceService")
public class NEWBatchconferenceServiceImpl extends ServiceImpl<NEWBatchconferenceMapper, BatchconferenceEntity> implements NEWBatchconferenceService {
    @Autowired
    private NEWBatchconferenceMapper batmapper;
    @Autowired
    private NEWConfernamelistMapper clistmapper;
    @Autowired
    ReportConfig reportConfig;
    @Autowired
    private BatchconferenceMapper batchconferenceMapper;

    @Override // com.tdqy.cadre.modular.profess.service.NEWBatchconferenceService
    public PageInfo queryPage(Map<String, Object> map) {
        Long deptId = SecurityUtils.getDeptId();
        int pageSize = 15;
        int pageNum = 1;
        if (map.get("pageSize") != null) {
            pageSize = Integer.parseInt((String) map.get("pageSize"));
        }
        if (map.get("pageNum") != null) {
            pageNum = Integer.parseInt((String) map.get("pageNum"));
        }
        //（已调整完毕） todo:修改查询方式，将查询sql全部迁移到xml文件中
        PageHelper.startPage(pageNum, pageSize);
        map.put("deptId",deptId);
        List<BatchconferenceEntity> list = batchconferenceMapper.getMeetingInfos3(map);
        return new PageInfo<>(list);
    }

    @Override // com.tdqy.cadre.modular.profess.service.NEWBatchconferenceService
    public PageInfo queryPageForHistory(Map<String, Object> map) {
        Long deptId = SecurityUtils.getDeptId();
        int pageSize = 15;
        int pageNum = 1;
        if (map.get("pageSize") != null) {
            pageSize = Integer.parseInt((String) map.get("pageSize"));
        }
        if (map.get("pageNum") != null) {
            pageNum = Integer.parseInt((String) map.get("pageNum"));
        }
        //（已调整完毕） todo:修改查询方式，将查询sql全部迁移到xml文件中
        PageHelper.startPage(pageNum, pageSize);
        map.put("deptId",deptId);
        List<BatchconferenceEntity> list = batchconferenceMapper.getMeetingInfos4(map);
        return new PageInfo<>(list);
    }

    @Override
    public List<BatchconferenceEntity> selectByAll() {
        return this.batmapper.selectByAll();
    }

    @Override // com.tdqy.cadre.modular.profess.service.NEWBatchconferenceService
    public List<BatchconferenceEntity> selectByMaps(Map<String, Object> map) {
        return this.batmapper.selectByMaps(map);
    }

    @Override // com.tdqy.cadre.modular.profess.service.NEWBatchconferenceService
    @Transactional(rollbackFor = {Exception.class})
    public int updateByPrimaryKeySelective(BatchconferenceEntity batchconferenceEntity) {
        return this.batmapper.updateByPrimaryKeySelective(batchconferenceEntity);
    }

    @Override
    public List<BatchconferenceEntity> selectByBWStata() {
        return this.batmapper.selectByBWStata();
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int updateAllStata() {
        return this.batmapper.updateAllStata();
    }

    @Override
    public int selectMaxBatch(String type, Long deptId) {
        return this.batmapper.selectMaxBatch(type, deptId);
    }

    @Override
    public int selectMaxOftenBatch(String type, Long deptId) {
        return this.batmapper.selectMaxOftenBatch(type, deptId);
    }

    @Override
    public String getMeetOftenName() {
        return new SimpleDateFormat("yyyy年MM月dd日").format(new Date()) + "常委会";
    }

    @Override
    public String getMeetName() {
        SecurityUtils.getDeptId();
        return new SimpleDateFormat("yyyy年MM月dd日").format(new Date()) + "部务会";
    }

    @Override
    public void getOftenWord(String oftenBathid, HttpServletResponse response) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        String outPath = "任免公示" + sdf.format(new Date()) + ".docx";
        String path = this.reportConfig.getWordOftenWord();
        List<ConfernamelistEntity> list = this.clistmapper.selectListByWord(oftenBathid);
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getA0101() != null) {
                if (sb.length() > 0) {
                    sb.append("\n\r");
                }
                sb.append("    " + list.get(i).getA0101());
            }
            if (list.get(i).getA0183() != null) {
                sb.append("，现任" + list.get(i).getA0183());
            }
            if (list.get(i).getAssumepost() != null) {
                sb.append("，拟任" + list.get(i).getAssumepost());
            }
            sb.append("。");
            if (list.get(i).getA0106() != null) {
                sb.append(sdf.format(list.get(i).getA0106()));
                sb.append("生");
            }
            if (list.get(i).getA0108() != null) {
                sb.append("，" + list.get(i).getA0108() + "人");
            }
            if (list.get(i).getA0190() != null) {
                if (list.get(i).getA0190().contains("\n")) {
                    sb.append("，" + list.get(i).getA0190().substring(0, list.get(i).getA0190().indexOf("\n")) + "学历");
                } else {
                    sb.append("，" + list.get(i).getA0190() + "学历");
                }
            } else if (list.get(i).getA0187() != null) {
                sb.append("，" + list.get(i).getA0187() + "学历");
            }
            if (list.get(i).getA0138() != null) {
                sb.append("，" + sdf.format(list.get(i).getA0138()));
                sb.append("参加工作");
            }
            if (list.get(i).getA0184() != null) {
                sb.append("，");
                sb.append(sdf.format(list.get(i).getA0184()));
                sb.append("加入中国共产党");
            }
            sb.append("。");
            sb.append("曾任xxxxxxx，xxxx，xxxx");
            if (list.get(i).getA0185() != null) {
                sb.append("，" + sdf.format(list.get(i).getA0185()));
                sb.append("任现职");
            }
            sb.append("。");
        }
        Map<String, String> map = new HashMap<>();
        map.put("${nr}", sb.toString());
        WordReplaceUtil.downloadWord(map, path, outPath, response);
    }

    @Override // com.tdqy.cadre.modular.profess.service.NEWBatchconferenceService
    public void updateStataById(String batchid) {
        this.clistmapper.updateStataById(batchid);
    }
}
